{
Traviesadora beta 0.2.0.13.33 (30.11.2008)
LastMod: 20.03.2009 by Prof1983

Copyright 2008, travieorama@googlemail.com

This file is part of the taviesadora project

taviesadora is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

taviesadora is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
}
unit ChatWin;

interface

uses
  Buttons, Windows, Types, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, ToolWin, ImgList, Menus, ShellApi,
  ABase, ChatDistributer, RosterGlobals,
  //VibSeqPaintManager, VibDistributer, VibWinFunction,
  HtmlLite, TraviesadoraCore,
  OleCtrls, Activex, ShlObj, Urlmon, Avatar, RosterViewer, RosterDB;

type
  TPanelPosition = (ppTop, ppBottom, ppNone);
  TExtraPanelRec = record
    Panel: TPanel;
    position: TPanelPosition;
    neededwidth: Integer;
  end;
  PExtraPanelRec = ^TExtraPanelRec;

  TFrmChat = class(TForm)
    pnlground: TPanel;
    pnlleft: TPanel;
    pnlsendchat: TPanel;
    memInText: TMemo;
    Panel4: TPanel;
    pnlVibRX: TPanel;
    imgbar: TImage;
    sbtnCloseRX: TSpeedButton;
    pnlhtml: TPanel;
    Panel2: TPanel;
    Image3: TImage;
    pnlAskAcception: TPanel;
    sbtnYes: TSpeedButton;
    sbtnNo: TSpeedButton;
    pnlright: TPanel;
    pnlAvatarMe: TPanel;
    imgshadow: TImage;
    imgAvatarMe: TImage;
    pnlAvatarPartner: TPanel;
    pnlVibTx: TPanel;
    Panel7: TPanel;
    Panel8: TPanel;
    Panel9: TPanel;
    Panel10: TPanel;
    pnlVibTXGraphics: TPanel;
    pbCoord: TPaintBox;
    imgBG: TImage;
    imgBarTX: TImage;
    sbtnHideforRX: TSpeedButton;
    ImageList1: TImageList;
    PaintTimer: TTimer;
    tmrCryptAni: TTimer;
    popupCryptConfig: TPopupMenu;
    Cancelauthentification1: TMenuItem;
    Settogoodencryption1: TMenuItem;
    Settoweakencryption1: TMenuItem;
    pppCopy: TPopupMenu;
    MItCopy1: TMenuItem;
    ppAtHandlerMenu: TPopupMenu;
    AddtoContactlist1: TMenuItem;
    SendEmail1: TMenuItem;
    imgAvatar_bgpic_me: TImage;
    imgAvatar_nopic_me: TImage;
    imgAvatar_nopic: TImage;
    imgAvatar_bgpic: TImage;
    pnlCaption: TPanel;
    lblnick: TLabel;
    lbl_uID: TLabel;
    Panel5: TPanel;
    Image5: TImage;
    sbtn_vib: TSpeedButton;
    sbtn_movie: TSpeedButton;
    Panel6: TPanel;
    Panel3: TPanel;
    Panel1: TPanel;
    Panel11: TPanel;
    Image6: TImage;
    pnlmiddle: TPanel;
    Image2: TImage;
    Image7: TImage;
    imgAvatarPartner: TImage;
    Image4: TImage;
    Image1: TImage;
    imgecke_l: TImage;
    imgecker: TImage;
    imgClose: TImage;
    pnlcryptani: TPanel;
    pnlcrypt: TPanel;
    bvlcryptani: TBevel;
    imgCryptAmpel: TImage;
    lbl_cryptstate: TLabel;
    imgrot: TImage;
    imggelb: TImage;
    imggruen: TImage;
    procedure ChatDistriDestroyed();
    procedure SetJID(JID: string);
    procedure SetRmtNick(RmtNick: string);
    procedure SetCryptState(CryptState: TCryptState);
    procedure NewInMessage(msg: string);
    procedure _scrollBottom();
    procedure AskForPlayingVib();
    procedure ClearAskingAcception();
    procedure DelAskingAcception(AcceptanceAction: TAcceptanceAction);
    procedure AddAskingAcception(AcceptanceAction: TAcceptanceAction);
    procedure AddWaitAcception(AcceptanceAction: TAcceptanceAction);
    procedure SetOutText(From, Text: string; Own: Boolean);
    procedure edtInputTextChange(Sender: TObject);
    procedure sbtnNoClick(Sender: TObject);
    procedure sbtnYesClick(Sender: TObject);
    procedure TbtnAskMovieClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure memInTextKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure tbtnvibClick(Sender: TObject);
    procedure SetVibRcvWind(SeqCount: Integer);
    procedure PaintTimerTimer(Sender: TObject);
    procedure pbCoordMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure pnlVibTXGraphicsCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean);
    procedure pbCoordMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pnlVibRXResize(Sender: TObject);
    procedure sbtnCloseRXClick(Sender: TObject);
    procedure sbtnHideforRXClick(Sender: TObject);
    procedure memInTextKeyPress(Sender: TObject; var Key: Char);
    procedure tmrCryptAniTimer(Sender: TObject);
    procedure Cancelauthentification1Click(Sender: TObject);
    procedure Settogoodencryption1Click(Sender: TObject);
    procedure Settoweakencryption1Click(Sender: TObject);
    procedure MItCopy1Click(Sender: TObject);
    procedure htmlLite1HotSpotClick(Sender: TObject; const SRC: String; var Handled: Boolean);
    procedure SendEmail1Click(Sender: TObject);
    procedure AddtoContactlist1Click(Sender: TObject);
    procedure EmbeddedWB1BeforeNavigate2(ASender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);

    procedure RegisterExtraPanel(Panel:TPanel;pos:TPanelPosition;neededwidth:integer);
    procedure UnRegisterExtraPanel(Panel:TPanel);
    procedure LoadAvatar();
    procedure sbtn_vibClick(Sender: TObject);
    procedure sbtn_movieClick(Sender: TObject);
    procedure pnlCaptionMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pnlCaptionMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure pnlCaptionMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgeckerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgeckerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure imgeckerMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgecke_lMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgecke_lMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure imgecke_lMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgCloseMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure imgCloseMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pnlCaptionResize(Sender: TObject);
    procedure imgCloseClick(Sender: TObject);
    procedure btn_sendClick(Sender: TObject);
  private
    btn_send: TButton;
    HtmlLite: THtmlLite;
  private
    FTravCore: ITraviesadoraCore;
    //VibSeqPaintManager: TVibSeqPaintManager;
    //VibDistri: TVibDistri;
    //vibFunction: TVibFunction;
    ChatDistri: TChatDistri;
    _AcceptanceAskList: TList;
    winclosing: Boolean;
    OutText: TStringList;
    ChatLinks: TStringList;
    _AcceptanceWaitList: TList;
    _receivedXIMNode: Boolean; //true if we have received a xhtml-im message
    _receivedMessage: Boolean; //true if we have received at least one message
    _supportsXIM: Boolean;     //true if caps advertises support
    _reply_id: string;
    _jid: string;
    _rmtNick: string;
    _MyName: string;
    _sent_composing: Boolean;
    _send_composing: Boolean;
    _check_event: Boolean;
    //_flash_ticks: Integer;
    cryptState: TCryptState;
    _AtAddress: string;
    _extrapnlList: TList;
    XOrig: Integer;
    YOrig: Integer;
    XResize: Integer;
    YResize: Integer;
  protected
    procedure DoCreate; override;
  public
    constructor Init(TravCore: ITraviesadoraCore; ChatDistri: TChatDistri;
      JabberID, rmtNick, MyName: AString; WaitAcceptance, AskAcception: TAcceptanceAction);
    //function GetPaintManager: TVibSeqPaintManager;
    //procedure SetVibWinFunction(vibFunction: TVibFunction);
    procedure ShowVibWinFunction();
    procedure SetVibDistriNil();
  end;

implementation

uses
  {Movie,} AddContactWin, Jabber,
  TraviesadoraApp;

{$R *.dfm}

{ TFrmChat }

procedure TFrmChat.AddAskingAcception(AcceptanceAction: TAcceptanceAction);
var
  p: PAcceptanceAction;
begin
  if AcceptanceAction <> aaNone then
  begin
    SetForegroundWindow(Application.MainForm.Handle);
    new(P);
    p^ := AcceptanceAction;
    _AcceptanceAskList.add(p);
    pnlAskAcception.Tag := _AcceptanceAskList.Count-1;
    pnlAskAcception.Visible := true;
    if _rmtNick = '' then
      pnlAskAcception.Caption := _jid+ ' asks for '
    else
      pnlAskAcception.Caption := _rmtNick+ ' asks for ';
    if AcceptanceAction = aaMovie then
      pnlAskAcception.Caption := pnlAskAcception.Caption+'watching a movie'
    else if AcceptanceAction = aaVib then
      pnlAskAcception.Caption := pnlAskAcception.Caption+'playing..';
  end
  else
    pnlAskAcception.Visible := false;
end;

procedure TFrmChat.ClearAskingAcception();
var
  p: PAcceptanceAction;
begin
  while (_AcceptanceAskList.Count > 0) do
  begin
    p := _AcceptanceAskList[0];
    dispose(p);
    _AcceptanceAskList.Delete(0);
  end;
  pnlAskAcception.Tag := -1;
  pnlAskAcception.Visible := false;
end;

procedure TFrmChat.DelAskingAcception(AcceptanceAction: TAcceptanceAction);
var
  p: PAcceptanceAction;
  i: Integer;
begin
  i := 0;
  while i < _AcceptanceAskList.Count do
  begin
    p := _AcceptanceAskList[pnlAskAcception.tag];
    if p^ = AcceptanceAction then
    begin
      dispose(p);
      _AcceptanceAskList.Delete(i);
      pnlAskAcception.Tag := -1;
      pnlAskAcception.Visible := false;
    end
    else
      inc(i);
  end;
end;

procedure TFrmChat.AddWaitAcception(AcceptanceAction: TAcceptanceAction);
var
  p: PAcceptanceAction;
begin
  if AcceptanceAction <> aaNone then
  begin
    new(P);
    p^ := AcceptanceAction;
    {pnlAskAcception.Visible := true;
    pnlAskAcception.Caption := displayName+ ' Asks for ';
    if AskAcception = aaMovie then
      pnlAskAcception.Caption := pnlAskAcception.Caption+'watching a movie'
    else if AskAcception = aaVib then
      pnlAskAcception.Caption := pnlAskAcception.Caption+'playing..';}
 end;
end;

constructor TFrmChat.Init(TravCore: ITraviesadoraCore; ChatDistri: TChatDistri;
  JabberID, rmtNick, MyName: AString; WaitAcceptance, AskAcception: TAcceptanceAction);
var
  rect: TRect;
  s: string;
begin
  FTravCore := TravCore;
  WinClosing := False;
  self.ChatDistri := ChatDistri;
  _AcceptanceAskList := TList.Create;
  _AcceptanceWaitList := TList.Create;
  OutText:=TStringList.create;
  _extrapnlList := TList.Create;
  _AtAddress := '';
  if (chatDistri.GetNetType = ntNobraNet) then
  begin
    s := rmtNick;
    delete(s,pos('/',s),length(s));
    lblnick.Caption := s;
  end
  else
    lblnick.Caption := rmtNick;

  if chatDistri.GetNetType <> ntNobraNet then
    lbl_uID.Caption := '('+JabberID+')'
  else
    lbl_uID.Caption := '';
  outtext.Text := '<body bgcolor=white>'{+'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'+
          ' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"'+
          ' width="499" height="428" id="player" align="middle"><param name="allowScriptAccess" value="sameDomain" />'+
          '<param name="movie" value="http://pc3.xtube.com/embed_player.swf?user_id=xtubehouse&idx=3&auto=0&video_id=KE8DqLDGmbT&clip_id=UZtRFFvqUYh" />'+
          '<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />'+
          '<embed src="http://pc3.xtube.com/embed_player.swf?user_id=xtubehouse&idx=3&auto=0&video_id=KE8DqLDGmbT&clip_id=UZtRFFvqUYh"'+
          ' quality="high" bgcolor="#ffffff" width="499" height="428" name="player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>'{};
  // EmbeddedWB1.LoadFromString(outtext.Text+'</body>');
  //Output.Lines.Add(outtext.Text);
  HtmlLite.LoadFromString(outtext.Text+'</body>','');
  ChatLinks := TStringList.Create;
  pnlVibRX.DoubleBuffered := True;
  {
  vibFunction := nil;
  VibDistri := nil;
  }
  pnlVibTx.Visible := False;
  //pnlVibTx.Height := 0;
  pnlVibRX.Height := 0;
  rect.Left := 0;
  rect.Top := 0;
  rect.Right := imgbar.Width;
  rect.Bottom := imgbar.Height;
  {
  VibSeqPaintManager := TVibSeqPaintManager.Create(imgbar.Canvas,rect,self,VibHardWareDriver);
  }
  AddAskingAcception(AskAcception);
  AddWaitAcception(WaitAcceptance);
  pnlAskAcception.Left := (Width div 2) - (pnlAskAcception.Width div 2);
  pnlAskAcception.top := (height div 2) - (pnlAskAcception.height div 2);
  _supportsXIM := false;
  _MyName := MyName;
  _reply_id := '';
  _jid := JabberID;
  _rmtNick := rmtNick;
  _check_event := false;
  _receivedMessage := false;
  _receivedXIMNode := false;
  // oldMsg := '';
  show;
  SetForegroundWindow(Application.MainForm.Handle);
  SetCryptState(chatDistri.GetCryptState);
  pnlVibRXResize(nil);
  LoadAvatar();
end;

procedure TFrmChat.edtInputTextChange(Sender: TObject);
begin
  if ((_sent_composing) and (memInText.Text = '')) then
  begin
    if ChatDistri <> nil then
      ChatDistri.sendComposing('',_jid);
      _sent_composing := false;
      _send_composing := true;
  end
  else if ((_send_composing) and (memInText.Text <> ''))then
  begin
    if ChatDistri <> nil then
       ChatDistri.sendComposing(_reply_id,_jid);
      _sent_composing := true;
      _send_composing := false;
  end;
end;

procedure TFrmChat.SetJID(JID: string);
begin
  _jid := JID;
end;

procedure TFrmChat.SetRmtNick(RmtNick: string);
begin
  _rmtNick := RmtNick;
end;

procedure TFrmChat._scrollBottom;
begin
//SendM
end;

procedure CutOutString(s:string;index:integer;var p:string;var m:string;var t:string);
var
  start,stop:integer;
begin
  start := index;
  while start > 0 do
  begin
    if s[start]= ' ' then
      break
    else
      dec(start);
  end;
  stop := index;
  while stop <= length(s) do
  begin
    if s[stop]= ' ' then
      break
    else
      inc(stop);
  end;
  p := s;
  delete(p,start+1,length(p));
  m := s;
  delete(m,stop,length(m));
  delete(m,1,start);
  t := s;
  delete(t,1,stop-1);
end;

procedure TFrmChat.SetOutText(From, Text: string; Own: boolean);
var
  status: Boolean;
  t: string;
  s: string;
  p: string;
  m: string;
  e: string;
begin
  if text = '' then exit;
  t := '';
  status := pos('<constatus>',Text)>0;
  if status then
    delete(text,1,pos('<constatus>',Text)+length('<constatus>')-1);
  if own then begin
    t := '<font color=red><b>';
  end else begin
     t := '<font color=blue><b>';
  end;
  if not status then begin
    t := t+ from+' ('+TimeToStr(now)+')</b></font>';
    outtext.add(t+'<br>');
  end;
  if own then
    t := '<font color=gray>'
  else begin
    if status then
      t := '<font color=gray>'
    else
      t := '<font color=black>'
  end;
  m := '';
  if (pos('://',text)>0) then
  begin
    CutOutString(text,pos('://',text),p,m,e);
  end
  else if (pos('www',text)>0) then
  begin
    CutOutString(text,pos('www',text),p,m,e);
  end
  else if (pos('@',text)>0) then
  begin
    CutOutString(text,pos('@',text),p,m,e);
  end;
  if m <> '' then
  begin
    m := '<a href='+m+'>'+m+'</a>';
    text := p+m+e;
  end;
  t := t + text+'</font>';
  outtext.add(t+'<br>');
  //EmbeddedWB1.LoadFromString(OutText.Text+'</body>');
  //EmbeddedWB1.Wait2;
  //EmbeddedWB1.ScrollToBottom;
  //EmbeddedWB1.ScrollBy(0,100000000);
  //Output.Lines.Add(OutText.Text);
  HtmlLite.LoadFromString(OutText.Text+'</body>','');
  HtmlLite.Position := HtmlLite.VScrollBarRange;
end;

procedure TFrmChat.NewInMessage(msg: string);
begin
  SetForegroundWindow(Application.MainForm.Handle);
  SetOutText(_rmtNick,msg,false);
  //oldMsg := memInText.Text;
  memInText.SetFocus;
  //Application.ProcessMessages;
  //memInText.Text := oldMsg;
end;

procedure TFrmChat.FormClose(Sender: TObject; var Action: TCloseAction);
var
  p: PAcceptanceAction;
begin
  Action := caFree;
  winclosing := true;
  while  (_AcceptanceAskList.Count > 0) do
  begin
    p := _AcceptanceAskList[0];
    if p^ <> aaNone then
      sbtnNo.Click
    else
    begin
      dispose(p);
      _AcceptanceAskList.Delete(0);
    end;
  end;

  while (_AcceptanceWaitList.Count > 0) do
  begin
    p := _AcceptanceWaitList[0];
    dispose(p);
    _AcceptanceWaitList.Delete(0);
  end;

  {
  if vibFunction <> nil then
    vibFunction.destroy;
  if VibDistri <> nil then
    VibDistri.destroy;
  VibSeqPaintManager.destroy;
  }
  if ChatDistri <> nil then
    ChatDistri.UnRegisterMsgCallback(self);
  _AcceptanceAskList.Free;
  _AcceptanceWaitList.Free;
  _extrapnlList.Free;
end;

procedure TFrmChat.memInTextKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if key = VK_RETURN then
  begin
    btn_sendclick(sender);
    memInText.Text := '';
    key := 0;
  end;
end;

procedure TFrmChat.sbtnNoClick(Sender: TObject);
var
  p: PAcceptanceAction;
begin
  if (_AcceptanceAskList.Count > pnlAskAcception.tag) then
  begin
    p := _AcceptanceAskList[pnlAskAcception.tag];
    if p^ = aaMovie then
    begin
      ChatDistri.MovieDenied;
    end else if p^ = aaVib then
    begin
      ChatDistri.VibDenied;
    end;
    dispose(p);
    _AcceptanceAskList.Delete(pnlAskAcception.Tag);
    pnlAskAcception.Tag := -1;
    pnlAskAcception.Visible := false;
  end;
end;

procedure TFrmChat.sbtnYesClick(Sender: TObject);
var
  p: PAcceptanceAction;
begin
  if (_AcceptanceAskList.Count > pnlAskAcception.tag) then
  begin
    p := _AcceptanceAskList[pnlAskAcception.tag];
    if p^ = aaMovie then
    begin
      ChatDistri.MovieAccepted(nil);
    end else if p^ = aaVib then
    begin
      ChatDistri.VibAccepted;
    end;
    dispose(p);
    _AcceptanceAskList.Delete(pnlAskAcception.Tag);
    pnlAskAcception.Tag := -1;
    pnlAskAcception.Visible := false;
  end;
end;

procedure TFrmChat.TbtnAskMovieClick(Sender: TObject);
var
  p: PAcceptanceAction;
begin
  if (_AcceptanceAskList.Count > pnlAskAcception.tag) and (pnlAskAcception.Visible) then
  begin
    p := _AcceptanceAskList[pnlAskAcception.tag];
    if p^ = aaMovie then begin
      ChatDistri.MovieAccepted(nil);
    end else if p^ = aaVib then begin
      ChatDistri.VibAccepted;
    end;
    dispose(p);
    _AcceptanceAskList.Delete(pnlAskAcception.Tag);
    pnlAskAcception.Tag := -1;
    pnlAskAcception.Visible := false;
  end else begin
    ChatDistri.AskForWatchingMovie(nil);
    NewInMessage('<constatus>Asked for watching movie..');
  end;
end;

procedure TFrmChat.ChatDistriDestroyed;
begin
  close;
  ChatDistri := nil;
end;

{function TFrmChat.GetPaintManager: TVibSeqPaintManager;
begin
  result := VibSeqPaintManager;
end;}

procedure TFrmChat.tbtnvibClick(Sender: TObject);
begin
  AskForPlayingVib;
  NewInMessage('<constatus>Asked for playing traviesito..');
end;

procedure TFrmChat.AskForPlayingVib;
begin
  {
  VibDistri := ChatDistri.AskForPlayingVib(nil) as TVibDistri;
  }
end;

procedure TFrmChat.SetVibRcvWind(SeqCount: integer);
begin
  if SeqCount = 0 then
  begin
    if pnlVibRX.Height > 0 then
    begin
      Height := height - pnlVibRX.Height;
      pnlVibRX.Height := 0;
    end;
  end
  else
  begin
    if pnlVibRX.Height = 0 then
    begin
      pnlVibRX.Height := 100;
      Height := Height + pnlVibRX.Height;
    end;
  end;
end;

(*procedure TFrmChat.SetVibWinFunction(vibFunction: TVibFunction);
begin
  self.vibFunction := vibFunction;
  if winclosing then exit;
  if vibFunction <> nil then begin
    pnlVibTXGraphics.DoubleBuffered := true;
    vibFunction.SetGraphics(imgBarTX,pbCoord,imgBG.Picture);
   { if pnlVibTx.Height = 0 then begin
      pnlVibTx.Height := 300;
      Height := Height + pnlVibTx.Height;
    end; }
  end else begin
    if pnlVibTx.Visible then begin
     // Height := height - pnlVibTx.Height;
      //pnlVibTx.Height := 0;
      UnRegisterExtraPanel(pnlVibTx);
    end;
    {if pnlVibTx.Height > 0 then begin   //unregister vibtx
      Height := height - pnlVibTx.Height;
      pnlVibTx.Height := 0;
    end;  }
  end;
end;*)

procedure TFrmChat.ShowVibWinFunction;
begin
  (*
  if vibFunction <> nil then
  begin
    pnlVibTXGraphics.DoubleBuffered := true;
    if not pnlVibTx.Visible then
    begin
      RegisterExtraPanel(pnlVibTx,ppNone,200);
      //pnlVibTx.Height := 300;
    {
      pnlVibTx.Height := 300;
      Height := Height + pnlVibTx.Height;}
    end;
  end else begin
    if pnlVibTx.Visible then begin
     // Height := height - pnlVibTx.Height;
      //pnlVibTx.Height := 0;
      UnRegisterExtraPanel(pnlVibTx);
    end;
  end;
  *)
end;

procedure TFrmChat.PaintTimerTimer(Sender: TObject);
begin
  {
  if vibFunction <> nil then
    vibFunction.TriggerPaint;
  if VibDistri <> nil then
  begin
    VibDistri.TriggerNewValue;
  end;
  }
end;

procedure TFrmChat.pbCoordMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  {
  if vibFunction <> nil then
    vibFunction.MouseMoveTo(x,y);
  }
end;

procedure TFrmChat.pbCoordMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
{var
  x1,y1:integer;
  cposorig:TPoint;}
begin
  {
  if vibFunction <> nil then
  begin
    GetCursorpos(cposorig);
    x1 := x;
    y1 := y;
    vibFunction.MouseClicked(x,y);
    SetCursorPos(cposorig.X +(x-x1),cposorig.y +(y-Y1));
  end;
  }
end;

procedure TFrmChat.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  {
  MousePos := pnlVibTXGraphics.ScreenToClient(MousePos);
  if (MousePos.X > 0) and (MousePos.X < pnlVibTXGraphics.Width) and
     (MousePos.y > 0) and (MousePos.y < pnlVibTXGraphics.height) then
     if vibFunction <> nil then
  begin
      vibFunction.MouseWheelDown;
      //Handled := true;
     end;
  }
end;

procedure TFrmChat.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  {
  MousePos := pnlVibTXGraphics.ScreenToClient(MousePos);
  if (MousePos.X > 0) and (MousePos.X < pnlVibTXGraphics.Width) and
     (MousePos.y > 0) and (MousePos.y < pnlVibTXGraphics.height) then
     if vibFunction <> nil then begin
      vibFunction.MouseWheelUp;
     // Handled := true;
     end;
  }
end;

procedure TFrmChat.pnlVibTXGraphicsCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  {
  if vibFunction <> nil then
  begin
    vibFunction.BarSizeChanged;
  end;
  sbtnHideforRX.Top := NewHeight-sbtnHideforRX.Height-2;
  sbtnHideforRX.Left := 2;
  }
end;

procedure TFrmChat.SetVibDistriNil;
begin
  {
  VibDistri := nil;
  DelAskingAcception(aaVib);
  }
end;

procedure TFrmChat.pnlVibRXResize(Sender: TObject);
begin
  {
  if VibSeqPaintManager <> nil then
  begin
    imgbar.Picture.Bitmap.Height := imgbar.Height;
    imgbar.Picture.Bitmap.Width := imgbar.Width;
    VibSeqPaintManager.SetRect(Rect(0,0,imgbar.Width,imgbar.Height));
    sbtnCloseRX.Left := pnlVibRX.Width-sbtnCloseRX.Width-2;
  end;
  imgCryptAmpel.Width := 200;
  imgCryptAmpel.Left := 1;
  imgCryptAmpel.top := 1;
  lbl_cryptstate.Width := imgCryptAmpel.Width;
  lbl_cryptstate.Left := imgCryptAmpel.Left;
  }
end;

procedure TFrmChat.sbtnCloseRXClick(Sender: TObject);
begin
  {
  if VibDistri <> nil then
    VibDistri.Destroy;
  }
end;

procedure TFrmChat.sbtnHideforRXClick(Sender: TObject);
begin
  {
  if vibFunction <> nil then
  begin
    if  vibFunction.ToggleRXBarAtRcverVisibility then
      sbtnHideforRX.Caption := 'Surprise'
    else
      sbtnHideforRX.Caption := 'Show everything';
  end;
  }
end;

procedure TFrmChat.memInTextKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then
    key := #0;
end;

procedure TFrmChat.SetCryptState(CryptState: TCryptState);
begin
  self.cryptState := CryptState;
  case CryptState of
    csNoEncryption:
      begin
        lbl_cryptstate.Caption := 'no Encryption';
        imgCryptAmpel.Picture := imgrot.Picture;
        tmrCryptAni.Enabled := false;
        pnlcryptani.Visible := false;
        memInText.color := clwindow;
        memInText.ReadOnly := false;
        Cancelauthentification1.Visible := false;
        Settogoodencryption1.Visible := true;
        Settoweakencryption1.Visible := true;
      end;
    csInitialising:
      begin
        lbl_cryptstate.Caption := 'Encryption starting..';
        imgCryptAmpel.Picture := nil;
        tmrCryptAni.Enabled := true;
        pnlcryptani.Visible := true;
        memInText.color := color;
        memInText.ReadOnly := true;
        Cancelauthentification1.Visible := false;
        Settogoodencryption1.Visible := false;
        Settoweakencryption1.Visible := false;
      end;
    csWeak:
      begin
        lbl_cryptstate.Caption := 'Weak Encryption';
        imgCryptAmpel.Picture := imggelb.Picture;
        tmrCryptAni.Enabled := false;
        pnlcryptani.Visible := false;
        memInText.color := clwindow;
        memInText.ReadOnly := false;
        Cancelauthentification1.Visible := false;
        Settogoodencryption1.Visible := true;
        Settoweakencryption1.Visible := false;
      end;
    csWaitingForQues:
      begin
        lbl_cryptstate.Caption := 'Waiting for Authentication..';
        imgCryptAmpel.Picture := imggelb.Picture;
        tmrCryptAni.Enabled := true;
        pnlcryptani.Visible := true;
        memInText.color := color;
        memInText.ReadOnly := true;
        Cancelauthentification1.Visible := true;
        Settogoodencryption1.Visible := false;
        Settoweakencryption1.Visible := false;
      end;
    csAuthenticated:
      begin
        lbl_cryptstate.Caption := 'Encrypted';
        imgCryptAmpel.Picture := imggruen.Picture;
        tmrCryptAni.Enabled := false;
        pnlcryptani.Visible := false;
        memInText.color := clwindow;
        memInText.ReadOnly := false;
        Cancelauthentification1.Visible := false;
        Settogoodencryption1.Visible := false;
        Settoweakencryption1.Visible := true;
      end;
    csChangingKey:
      begin
        lbl_cryptstate.Caption := 'Changing Key..';
        imgCryptAmpel.Picture := imggruen.Picture;
        tmrCryptAni.Enabled := true;
        pnlcryptani.Visible := true;
        memInText.color := clwindow;
        memInText.ReadOnly := false;
        Cancelauthentification1.Visible := false;
        Settogoodencryption1.Visible := false;
        Settoweakencryption1.Visible := false;
      end;
  end;
  lbl_cryptstate.Repaint;
  imgCryptAmpel.Repaint
end;

procedure TFrmChat.tmrCryptAniTimer(Sender: TObject);
begin
  if (pnlcryptani.tag = 0) then
  begin
    pnlcryptani.Width := pnlcryptani.Width+2;
    if pnlcryptani.Width+2 >= bvlcryptani.Width then
     pnlcryptani.Tag := 1;
  end
  else
  begin
    pnlcryptani.left := pnlcryptani.left+2;
    pnlcryptani.Width := pnlcryptani.Width-2;
    if (pnlcryptani.Width <= 0) then
    begin
     pnlcryptani.Tag := 0;
     pnlcryptani.Left := bvlcryptani.Left+1;
    end;
  end;
end;

procedure TFrmChat.Cancelauthentification1Click(Sender: TObject);
begin
  ChatDistri.SetToWantedCrypt(csweak);
end;

procedure TFrmChat.Settogoodencryption1Click(Sender: TObject);
begin
  ChatDistri.SetToWantedCrypt(csAuthenticated);
end;

procedure TFrmChat.Settoweakencryption1Click(Sender: TObject);
begin
  ChatDistri.SetToWantedCrypt(csweak);
end;

procedure TFrmChat.MItCopy1Click(Sender: TObject);
begin
  {
  //EmbeddedWB1.Copy;
  htmlLite1.CopyToClipboard;
  }
end;

procedure TFrmChat.htmlLite1HotSpotClick(Sender: TObject; const SRC: String; var Handled: Boolean);
var
  atpos: Integer;
  protpos: Integer;
  s: string;
  mpos: TPoint;
begin
  protpos := pos('://',src);
  atpos := pos('@',src);
  s := src;
  if (protpos = 0) and (atpos=0) then
    s := 'http://'+src
  else if (atpos>0) then
  begin
    mpos := Mouse.CursorPos;
    _AtAddress := s;
    ppAtHandlerMenu.Popup(mpos.X,mpos.y);
    s := '';
  end;
  if s <> '' then
    shellexecute(Application.MainForm.Handle,'open',Pchar(S),'','',SW_SHOW);
  handled := true;
end;

procedure TFrmChat.SendEmail1Click(Sender: TObject);
begin
  ShellExecute(Application.MainForm.Handle,'open',Pchar('mailto:'+_AtAddress),'','',SW_SHOW);
end;

procedure TFrmChat.AddtoContactlist1Click(Sender: TObject);
begin
  FrmAddContact := TFrmAddContact.Create(nil);
  FrmAddContact.Init(FTravCore);
  FrmAddContact.JabberID := _AtAddress;
  FrmAddContact.Show();
end;

procedure TFrmChat.EmbeddedWB1BeforeNavigate2(ASender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
var
  atpos: Integer;
  protpos: Integer;
  s: string;
  src: string;
  mpos: TPoint;
begin
  src := url;
  delete(src,1,pos(':',src));
  if src='blank' then exit;
  protpos := pos('://',src);
  atpos := pos('@',src);
  s := src;
  if (protpos = 0) and (atpos=0) then
    s := 'http://'+src
  else if (atpos>0) then begin
    mpos := Mouse.CursorPos;
    _AtAddress := s;
    ppAtHandlerMenu.Popup(mpos.X,mpos.y);
    s := '';
  end;
  if s <> '' then
    shellexecute(Application.MainForm.Handle,'open',Pchar(S),'','',SW_SHOW);
  cancel := true;
end;

procedure TFrmChat.RegisterExtraPanel(Panel: TPanel; pos: TPanelPosition;neededwidth:integer);
var
  prec:PExtraPanelRec;
  i:integer;
  n:integer;
begin
  i := neededwidth;
  for n := 0 to _extrapnlList.Count-1 do
  begin
    prec := _extrapnlList[n];
    if prec.neededwidth > i then
      i := prec.neededwidth;
  end;
  if pnlright.Visible then
    Width := Width+(i-pnlright.Width)
  else
    Width := Width+i;
  pnlright.Width := i;
  new(prec);
  prec.Panel := panel;
  prec.position := pos;
  prec.neededwidth := neededwidth;
  pnlmiddle.Left := 0;
  if _extrapnlList.Count = 0 then
  begin
   // Width := Width+neededwidth;
    pnlright.Visible := true;
  end;
  _extrapnlList.Add(prec);
  Panel.Parent := pnlright;
  if pos = ppNone then begin
    Panel.Top := 0;
    Panel.Align := alBottom;
  end;
  if pos = ppTop then begin
    Panel.Align := alTop;
  end;
  if pos = ppBottom then begin
    Panel.Top := pnlright.Height;
    Panel.Align := alBottom;
  end;
  pnlcrypt.Top := height;
  Panel.Visible := true;
end;

procedure TFrmChat.UnRegisterExtraPanel(Panel: TPanel);
var
  prec:PExtraPanelRec;
  i:integer;
  n:integer;
  valid:boolean;
  foundindex:integer;
begin
  i := 0;
  //valid := false;
  foundindex :=-1;
  for n := 0 to _extrapnlList.Count-1 do
  begin
    prec := _extrapnlList[n];
    if prec.Panel = Panel then
    begin
      foundindex := n;
      //break;
    end
    else
    begin
      if prec.neededwidth > i then
        i := prec.neededwidth;
    end;

    if foundindex>-1 then
    begin
      if pnlright.Visible then
        Width := Width+(i-pnlright.Width)
      else
        Width := Width+i;
      pnlright.Width := i;
      prec.Panel.Visible := false;
      _extrapnlList.Delete(foundindex);
      Dispose(prec);
      if _extrapnlList.Count = 0 then
      begin
        pnlright.Visible := false;
        //Width := Width-200;
      end;
    end;
  end;
end;

procedure TFrmChat.LoadAvatar;
var
  up: TUserProfile;
  s: string;
begin
  up := FTravCore.Jabber.GetmyUserProfile;
  if Assigned(up) then
  begin;
    PaintAvatar(imgAvatarMe, imgAvatar_bgpic_me, imgAvatar_nopic_me, FTravCore.Jabber, nil);
    RegisterExtraPanel(pnlAvatarMe, ppBottom, 150);
  end;
  s := _jid;
  delete(s, Pos('/',s), Length(s));
  up := FTravCore.Jabber.AvatarCache.Find(s);
  if Assigned(up) then
  begin;
    PaintAvatar(imgAvatarPartner, imgAvatar_bgpic, imgAvatar_nopic, FTravCore.Jabber, up);
    RegisterExtraPanel(pnlAvatarPartner, ppTop, 150);
  end;
end;

procedure TFrmChat.sbtn_vibClick(Sender: TObject);
begin
  AskForPlayingVib;
  NewInMessage('<constatus>Asked for playing traviesito..');
end;

procedure TFrmChat.sbtn_movieClick(Sender: TObject);
var
  p: PAcceptanceAction;
begin
  if (_AcceptanceAskList.Count > pnlAskAcception.tag) and (pnlAskAcception.Visible) then
  begin
    p := _AcceptanceAskList[pnlAskAcception.tag];
    if p^ = aaMovie then
    begin
      ChatDistri.MovieAccepted(nil);
    end
    else if p^ = aaVib then
    begin
      ChatDistri.VibAccepted;
    end;
    dispose(p);
    _AcceptanceAskList.Delete(pnlAskAcception.Tag);
    pnlAskAcception.Tag := -1;
    pnlAskAcception.Visible := false;
  end
  else
  begin
    ChatDistri.AskForWatchingMovie(nil);
    NewInMessage('<constatus>Asked for watching movie..');
  end;
end;

procedure TFrmChat.pnlCaptionMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  XOrig := +x;
  YOrig := +y;
  pnlCaption.Tag := 1;
end;

procedure TFrmChat.pnlCaptionMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if pnlCaption.Tag = 1 then
  begin
    left := +left-(XOrig-x);
    top := +top-(YOrig-y);
  end;
end;

procedure TFrmChat.pnlCaptionMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  pnlCaption.Tag := 0;
end;

procedure TFrmChat.imgeckerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  XResize := +x;
  YResize := +y;
  imgecker.Tag := 1;
end;

procedure TFrmChat.imgeckerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if imgecker.Tag = 1 then
  begin
    Width := +Width-(XResize-x);
    Height := +Height-(YResize-y);
  end;
end;

procedure TFrmChat.imgeckerMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  imgecker.Tag := 0;
end;

procedure TFrmChat.imgecke_lMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  XResize := +x;
  YResize := +y;
  imgecke_l.Tag := 1;
end;

procedure TFrmChat.imgecke_lMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  if imgecke_l.Tag = 1 then
  begin
    Width := Width+(XResize-x);
    height := height-(YResize-y);
    left := +left-(XResize-x);
  end;
end;

procedure TFrmChat.imgecke_lMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  imgecke_l.Tag := 0;
end;

procedure TFrmChat.imgCloseMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  imgClose.Top := imgClose.Top+1;
  imgClose.left := imgClose.left+1;
  imgClose.Tag := 1;
end;

procedure TFrmChat.imgCloseMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if imgClose.Tag = 1 then
  begin
    imgClose.Top := imgClose.Top-1;
    imgClose.left := imgClose.left-1;
    imgClose.Tag := 0;
  end;
end;

procedure TFrmChat.pnlCaptionResize(Sender: TObject);
begin
  imgClose.Left := pnlCaption.Width - 20;
end;

procedure TFrmChat.imgCloseClick(Sender: TObject);
begin
  close;
end;

procedure TFrmChat.btn_sendClick(Sender: TObject);
begin
  if (ChatDistri <> nil) then
  begin
    ChatDistri.SendMessage(memInText.Text,self);
    SetOutText(_MyName,memInText.Text,true);
    memInText.Clear;
  end;
end;

procedure TFrmChat.DoCreate;
begin
  inherited;

  btn_send := TButton.Create(Self);
  btn_send.Parent := Panel4;
  btn_send.Left := 8;
  btn_send.Top := 16;
  btn_send.Width := 33;
  btn_send.Height := 25;
  btn_send.Caption := 'send';
  btn_send.Font.Charset := DEFAULT_CHARSET;
  btn_send.Font.Color := clWindowText;
  btn_send.Font.Height := -11;
  btn_send.Font.Name := 'MS Sans Serif';
  btn_send.Font.Style := [];
  btn_send.TabOrder := 0;
  btn_send.TabStop := True;
  btn_send.OnClick := btn_sendClick;

  HtmlLite := THtmlLite.Create(Self);
  HtmlLite.Parent := pnlhtml;
  HtmlLite.Left := 1;
  HtmlLite.Top := 1;
  HtmlLite.Width := 316;
  HtmlLite.Height := 306;
  HtmlLite.OnHotSpotClick := htmlLite1HotSpotClick;
  HtmlLite.TabOrder := 0;
  HtmlLite.Align := alClient;
  HtmlLite.PopupMenu := pppCopy;
  HtmlLite.DefBackground := clWindow;
  HtmlLite.BorderStyle := htNone;
  HtmlLite.HistoryMaxCount := 0;
  HtmlLite.DefFontName := 'Times New Roman';
  HtmlLite.DefPreFontName := 'Courier New';
  HtmlLite.NoSelect := False;
  HtmlLite.ScrollBars := ssVertical;
  HtmlLite.CharSet := DEFAULT_CHARSET;
  HtmlLite.htOptions := [];
end;

end.

